package jdbcEx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionEx {

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		ConnectionEx connectionEx = new ConnectionEx();
		
		connectionEx.updateDB();
	 	
		
	}
 
	private void insertDB() throws SQLException
	{  
		Connection connection = null;
		Statement statement = null;
	 
		try {
			Class.forName("com.mysql.jdbc.Driver");

			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/world", "root", "root");

			statement = connection.createStatement();

			int rowsInserted  = statement
					.executeUpdate("insert into city(ID,name,countrycode,district,population) values(9999,'SFO','USA','SFOCOUNTY',30000)");

			 System.out.println(" Number of Rows Inserted is - "+rowsInserted);

			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally {

		 

			if (statement != null) {
				statement.close();
			}

			if (connection != null) {

				connection.close();
			}
		}

	
	
		
	}
	
	
	
	
	
	
	private void updateDB() throws SQLException
	{  
		Connection connection = null;
		Statement statement = null;
	 
		try {
			Class.forName("com.mysql.jdbc.Driver");

			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/world", "root", "root");

			statement = connection.createStatement();

			int rowsUpdated  = statement
					.executeUpdate("update city set Population ='40000' where CountryCode ='USA';");

			 System.out.println(" Number of Rows Updated is - "+rowsUpdated);

			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally {

		 

			if (statement != null) {
				statement.close();
			}

			if (connection != null) {

				connection.close();
			}
		}

	
	
		
	}
	 
	
	
	private void selectExample() throws SQLException
	{
	 
		Connection connection = null;
		Statement statement = null;
		// Question
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");

			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/world", "root", "root");

			statement = connection.createStatement();

			resultSet = statement
					.executeQuery("select ID,CountryCode,Population from city");

			while (resultSet.next()) {
				System.out
						.println(resultSet.getInt("Population") + "  "
								+ resultSet.getInt(1) + "    "
								+ resultSet.getString(3));

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		finally {

			if (resultSet != null) {
				resultSet.close();
			}

			if (statement != null) {
				statement.close();
			}

			if (connection != null) {

				connection.close();
			}
		} 
	}

}
